# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/3/1
# @File   : EC03_LoanDebtInterestCnt.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:贷款欠息次数
code:EC03_LoanDebtInterestCnt
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ
type:常用指标
dataSourceType:在线指标 description:这是获取 EC03 字段的数据脚本
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def EC03_LoanDebtInterestCnt():
    try:
        # 未结清欠息明细
        ECRUnpaidDebtInterest = getTable('ECRUnpaidDebtInterest')
        # 已结清欠息明细
        ECRPaidDebtInterest = getTable('ECRPaidDebtInterest')

        if ECRPaidDebtInterest is None and ECRUnpaidDebtInterest is None:
            return u'缺失值'
        else:
            if ECRPaidDebtInterest == [{}]:
                Factor1 = 0
            elif ECRPaidDebtInterest is None:
                Factor1 = None
            else:
                flag = []
                Factor1 = 0
                for i in ECRPaidDebtInterest:
                    if i.get('ReportNo') not in null_type_list and i.get('Paidbalancedate') not in null_type_list \
                            and isfloat(i.get('Owebalancetormb')):
                        Factor1 += 1
                    else:
                        flag.append(None)
                if len(flag) == len(ECRPaidDebtInterest):
                    Factor1 = None

            if ECRUnpaidDebtInterest == [{}]:
                Factor2 = 0
            elif ECRUnpaidDebtInterest is None:
                Factor2 = None
            else:
                flag = []
                Factor2 = 0
                for i in ECRUnpaidDebtInterest:
                    if i.get('ReportNo') not in null_type_list and isfloat(i.get('Owebalancetormb')) \
                        and i.get('BalanceChangedate') not in null_type_list:
                        Factor2 += 1
                    else:
                        flag.append(None)
                if len(flag) == len(ECRUnpaidDebtInterest):
                    Factor2 = None

            if Factor1 == None and Factor2 == None:
                return u'缺失值'
            else:
                if Factor1 == None:
                    Factor1 = 0
                if Factor2 == None:
                    Factor2 = 0
                return Factor1 + Factor2
    except:
        return u'缺失值'
result = EC03_LoanDebtInterestCnt()